package com.ca.project.web.Exception;

import com.ca.project.core.Exception.BusinessException;
import com.ca.project.core.Exception.FrameworkException;
import com.ca.project.core.Respond.R;
import com.ca.project.core.Respond.ResponseCode;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

/**
 * 全局异常处理
 * 对controller进行切面
 * 转换报错结果显示
 *
 * @PACKAGE_NAME: com.ca.project
 * @NAME: WebExceptionHandler
 * @USER: ASUS
 * @DATE: 2023/8/25
 * @TIME: 下午 04:59
 **/
@RestControllerAdvice
public class WebExceptionHandler {

    @ExceptionHandler(value = BusinessException.class)
    public R rPanExceptionHandler(BusinessException e) {
        return R.fail(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public R methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        ObjectError objectError = e.getBindingResult().getAllErrors().stream().findFirst().get();
        return R.fail(ResponseCode.ERROR_PARAM.getCode(), objectError.getDefaultMessage());
    }

    @ExceptionHandler(value = ConstraintViolationException.class)
    public R constraintViolationExceptionHandler(ConstraintViolationException e) {
        ConstraintViolation constraintViolation = e.getConstraintViolations().stream().findFirst().get();
        return R.fail(ResponseCode.ERROR_PARAM.getCode(), constraintViolation.getMessage());
    }

    @ExceptionHandler(value = MissingServletRequestParameterException.class)
    public R missingServletRequestParameterExceptionHandler(MissingServletRequestParameterException e) {
        return R.fail(ResponseCode.ERROR_PARAM.getCode(), ResponseCode.ERROR_PARAM.getDesc());
    }

    @ExceptionHandler(value = IllegalStateException.class)
    public R illegalStateExceptionHandler(IllegalStateException e) {
        return R.fail(ResponseCode.ERROR_PARAM.getCode(), ResponseCode.ERROR_PARAM.getDesc());
    }

    @ExceptionHandler(value = BindException.class)
    public R bindExceptionHandler(BindException e) {
        FieldError fieldError = e.getBindingResult().getFieldErrors().stream().findFirst().get();
        return R.fail(ResponseCode.ERROR_PARAM.getCode(), fieldError.getDefaultMessage());
    }

    @ExceptionHandler(value = FrameworkException.class)
    public R frameworkExceptionHandler(FrameworkException e) {
        e.printStackTrace();
        return R.fail(ResponseCode.ERROR.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = RuntimeException.class)
    public R runtimeExceptionHandler(RuntimeException e) {
        e.printStackTrace();
        return R.fail(ResponseCode.ERROR.getCode(), e.getMessage());
    }

}
